<?php

// client statuses
$ClientStatuses = array (
	0 => 'Active',
	1 => 'Blocked',
	2 => 'Suspended [Traffic]',
	3 => 'Suspended [Expired]',
	4 => 'Waiting Activation'
);

$DomainsStatuses = array (
	0 => 'Unapproved Registration',
	1 => 'Unapproved Renewal',
	2 => 'Unapproved Transfer',
	3 => 'Unapproved Change',
	4 => 'Active',
	5 => 'Rejected',
	6 => 'Suspended',
	7 => 'Transferring',
	8 => 'Transfer Failed',
	9 => 'Domain Expired'
);

$orderStatuses = array(
	1 => 'Refund',
	2 => 'Fraud',
	64 => 'Chargeback Fraud',
	4 => 'MBG',
	8 => 'Chargeback',
	16 => 'Verified Phone',
	32 => 'Verified Other',
	0 => 'Not Verified',
	128 => 'Possible Fraud',
	256 => 'Awaiting Res.',
	512 => 'Received Res.',
	1024 => 'Partial Refund',
	2048 => 'Test Account'
);
	
$xajaxUrl = $baseURL.'/'.$webDIR.'/?sid='.$sid.'&action=manageEmailNotifications';

// sve: Xajax functions

function getClientDetailsByUsername ($clientUsername, $resellerId) {
	$master_db = $GLOBALS['master_db'];
	
	$clientUsername = $master_db->escapeStr($clientUsername);
	$resellerId = (int) $resellerId;
	
	$query = <<<echo
SELECT client_id, username, firstname, lastname, email, date, server, blocked FROM clients WHERE username = '$clientUsername' AND reseller_id = $resellerId LIMIT 1;
echo;
	$master_db->query($query);
	// get client details
	$client = $master_db->getNext();
	
	// get client domains
	$query = <<<echo
SELECT registerdomain.sld as sld, registerdomain.tld as tld, registerdomain.flags as flags, purchases.expire_date as expire_date FROM purchases, registerdomain WHERE purchases.client_id = $client[client_id] AND purchases.purchase_id = registerdomain.purchase_id;
echo;
	$master_db->query($query);
	// get client domains
	$clientDomains['domains'] = $master_db->getAll();
	
	// create Db hosting sql object
	$dbPh = createDBHostingObject ($clientUsername);
	
	$query = <<<echo
SELECT expire_date FROM plans WHERE user_id = $client[client_id];
echo;

	$dbPh->query($query);
	$clientHosting = $dbPh->getNext();
	
	return array_merge($client, $clientHosting, $clientDomains);
}

function getClientDetails ($clientString) {
	// include SQL Time class
	include_once(LIBS_DIR.'SQL/sqltime.class.php');
	$timeClass = new SQLTime;
	// domains array
	$Domains = array ();
	// details array
	$Details = array ();
	// html string to display
	$htmlStart = '<table cellpadding="0" cellspacing="0" border="0" class="bTable" width="100%">
	<tr>
		<th colspan="2">Account Info</th>
		<th colspan="2">Domains Info</th>
	</tr>';
	$htmlEnd = '</table>';
	
	$resellerId = $GLOBALS['id'];
	
	$objResponse = new xajaxResponse();
	
	// get client username
	$clientUsername = explode(':', $clientString);
	// get client details
	$clientDetails = getClientDetailsByUsername ($clientUsername[0], $resellerId);
	
	foreach ($clientDetails['domains'] as $key => $value) {
		$Domains[] = '<td align="left" width="25%">Domain:</td>
			<td align="left" width="25%">'.$value['sld'].'.'.$value['tld'].'</td>';
		$Domains[] = '<td align="left">Expiration Date:</td>
			<td align="left">'.$timeClass->Time2Stamp($value['expire_date'], 'F d, Y').'</td>';
		$Domains[] = '<td align="left">Domain Status:</td>
			<td align="left">'.$GLOBALS['DomainsStatuses'][$value['flags']].'</td>';
	}
	
	// set different colours for different account statuses
	switch ($clientDetails['blocked']) {
		case 1: // blocked
			$span1 = '<span style="color: red;">';
			$span2 = '</span>';
			break;
		case 2: // blocked
			$span1 = '<span style="color: green;">';
			$span2 = '</span>';
			break;
		case 3: // blocked
			$span1 = '<span style="color: green;">';
			$span2 = '</span>';
			break;
		case 4: // blocked
			$span1 = '<span style="color: green;">';
			$span2 = '</span>';
			break;
		default:
			$span1 = '';
			$span2 = '';
			break;
	}
	
	// assign real fields with values
	$details = array (
		'Username' => $span1.$clientDetails['username'].$span2,
		'Full Name' => $clientDetails['firstname'].' '.$clientDetails['lastname'],
		'E-mail' => $clientDetails['email'],
		'Creation Date' => $timeClass->Time2Stamp($clientDetails['date'], 'F d, Y'),
		'Expiration Date' => $span1.$timeClass->Time2Stamp($clientDetails['expire_date'], 'F d, Y').$span2,
		'Located @' => $clientDetails['server'],
		'Account Status' => $span1.$GLOBALS['ClientStatuses'][$clientDetails['blocked']].$span2,
	);
	
	foreach ($details as $key => $value) {
		$Details[] = '<td align="left" width="25%">'.ucwords($key).':</td>
			<td align="left" width="25%">'.$value.'</td>';
	}
	
	// which array has more values?
	$iCount = (count($Domains) > count($Details)) ? count($Domains) : count($Details);
	for ($i = 0; $i < $iCount; $i++) {
		$htmlStart .= '<tr>';
		$htmlStart .= (!empty($Details[$i])) ? $Details[$i] : '<td colspan="2" width="50%"></td>';
		$htmlStart .= (!empty($Domains[$i])) ? $Domains[$i] : '<td colspan="2" width="50%"></td>';
		$htmlStart .= '</tr>';
	}
	
	// assign the full html
	$html = $htmlStart.$htmlEnd;
	
	if (empty($clientString)) {
		$objResponse->addAssign('clientDetails', 'innerHTML', '');
	}
	else {
		$objResponse->addAssign('clientDetails', 'innerHTML', $html);
	}
	
	return $objResponse->getXML();
}

$xajaxRegisterFunctions = array (
	'getClientDetails' => XAJAX_GET,
);

?>